package com.castle.fortress.admin.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.pay.dto.PayAliConfigDto;
import com.castle.fortress.admin.pay.entity.PayAliConfigEntity;
import com.castle.fortress.admin.pay.enums.PayTypeEnum;
import com.castle.fortress.admin.pay.service.PayAliConfigService;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 支付宝支付配置表 控制器
 *
 * @author castle
 * @since 2022-07-07
 */
@Api(tags="支付宝支付配置表管理控制器")
@Controller
public class PayAliConfigController {
    @Autowired
    private PayAliConfigService payAliConfigService;

    /**
     * 支付宝支付配置表的分页展示
     * @param payAliConfigDto 支付宝支付配置表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("支付宝支付配置表分页展示")
    @GetMapping("/pay/payAliConfig/page")
    @ResponseBody
    @RequiresPermissions("pay:payAliConfig:pageList")
    public RespBody<IPage<PayAliConfigDto>> pagePayAliConfig(PayAliConfigDto payAliConfigDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<PayAliConfigDto> page = new Page(pageIndex, pageSize);
        IPage<PayAliConfigDto> pages = payAliConfigService.pagePayAliConfig(page, payAliConfigDto);

        return RespBody.data(pages);
    }

    /**
     * 支付宝支付配置表的列表展示
     * @param payAliConfigDto 支付宝支付配置表实体类
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表列表展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("支付宝支付配置表列表展示")
    @GetMapping("/pay/payAliConfig/list")
    @ResponseBody
    @RequiresPermissions("pay:payScene:configEdit")
    public RespBody<List<PayAliConfigDto>> listPayAliConfig(PayAliConfigDto payAliConfigDto){
        List<PayAliConfigDto> list = payAliConfigService.listPayAliConfig(payAliConfigDto);
        for(PayAliConfigDto dto:list){
            PayTypeEnum typeEnum = PayTypeEnum.getByCode(dto.getPayTypeCode());
            dto.setPayTypeName(typeEnum.getDesc());
        }
        return RespBody.data(list);
    }

    /**
     * 支付宝支付配置表保存
     * @param payAliConfigDto 支付宝支付配置表实体类
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表保存", operType = OperationTypeEnum.INSERT)
    @ApiOperation("支付宝支付配置表保存")
    @PostMapping("/pay/payAliConfig/save")
    @ResponseBody
    @RequiresPermissions("pay:payAliConfig:save")
    public RespBody<String> savePayAliConfig(@RequestBody PayAliConfigDto payAliConfigDto){
        if(payAliConfigDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        PayAliConfigEntity payAliConfigEntity = ConvertUtil.transformObj(payAliConfigDto,PayAliConfigEntity.class);
        payAliConfigService.initStatus(payAliConfigEntity);
        if(payAliConfigService.save(payAliConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付宝支付配置表编辑
     * @param payAliConfigDto 支付宝支付配置表实体类
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表编辑", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("支付宝支付配置表编辑")
    @PostMapping("/pay/payAliConfig/edit")
    @ResponseBody
    @RequiresPermissions(value = {"pay:payAliConfig:edit","pay:payScene:configEdit"}, logical = Logical.OR)
    public RespBody<String> updatePayAliConfig(@RequestBody PayAliConfigDto payAliConfigDto){
        if(payAliConfigDto == null || payAliConfigDto.getId() == null || payAliConfigDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        PayAliConfigEntity payAliConfigEntity = ConvertUtil.transformObj(payAliConfigDto,PayAliConfigEntity.class);
        payAliConfigService.initStatus(payAliConfigEntity);
        if(payAliConfigService.updateById(payAliConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付宝支付配置表删除
     * @param id
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("支付宝支付配置表删除")
    @PostMapping("/pay/payAliConfig/delete")
    @ResponseBody
    @RequiresPermissions("pay:payAliConfig:delete")
    public RespBody<String> deletePayAliConfig(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(payAliConfigService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 支付宝支付配置表批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表批量删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("支付宝支付配置表批量删除")
    @PostMapping("/pay/payAliConfig/deleteBatch")
    @ResponseBody
    @RequiresPermissions("pay:payAliConfig:deleteBatch")
    public RespBody<String> deletePayAliConfigBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(payAliConfigService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付宝支付配置表详情
     * @param id 支付宝支付配置表id
     * @return
     */
    @CastleLog(operLocation = "支付宝支付配置表详情", operType = OperationTypeEnum.QUERY)
    @ApiOperation("支付宝支付配置表详情")
    @GetMapping("/pay/payAliConfig/info")
    @ResponseBody
    @RequiresPermissions("pay:payAliConfig:info")
    public RespBody<PayAliConfigDto> infoPayAliConfig(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        PayAliConfigEntity payAliConfigEntity = payAliConfigService.getById(id);
        PayAliConfigDto payAliConfigDto = ConvertUtil.transformObj(payAliConfigEntity,PayAliConfigDto.class);

        return RespBody.data(payAliConfigDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "支付宝支付配置表导出", operType = OperationTypeEnum.EXPORT)
	@PostMapping("/pay/payAliConfig/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<PayAliConfigDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<PayAliConfigDto> list = payAliConfigService.listPayAliConfig(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
